package com.lee.lxl.idcard.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.lee.lxl.idcard.Bean.ChatBean;
import com.lee.lxl.idcard.R;
import com.lee.lxl.idcard.adapter.ChatAdapter;
import com.lee.lxl.idcard.tools.SPUtils;
import com.lee.lxl.idcard.urls.Urls;
import com.zhy.autolayout.AutoLayoutActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatActivity extends AutoLayoutActivity {

    @Bind(R.id.listview)
    ListView listview;
    @Bind(R.id.et_chat)
    EditText etChat;
    @Bind(R.id.bt_chat)
    Button btChat;

    private ChatAdapter adapter;
    private ChatBean bean;
    private List<ChatBean> list;
    private AbHttpUtil mAbHttpUtil = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        bean = new ChatBean();
        list = new ArrayList<ChatBean>();
        SPUtils.put(ChatActivity.this,"chat","true");
//        bean.setText("你好！");
//        list.add(bean);
        adapter = new ChatAdapter(ChatActivity.this,list);
        listview.setAdapter(adapter);
        mAbHttpUtil = AbHttpUtil.getInstance(this);
        mAbHttpUtil.setTimeout(10000);
        mAbHttpUtil.setEncode("UTF-8");
    }

    @OnClick(R.id.bt_chat)
    public void onClick() {
        // 绑定参数
        AbRequestParams params = new AbRequestParams();
        params.put("key", "4ea6e557fe6d47d41ed4b548f27486a9");
        params.put("info",etChat.getText().toString());
        bean.setMytext(etChat.getText().toString());
        bean.setText("");
        list.add(bean);
        adapter.notifyDataSetChanged();
        //etChat.setText("");
        Log.e("ChatActivity", "list.size()1111111111     :" + list.size());
        //list.clear();

        mAbHttpUtil.get(Urls.CHAT, params, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {
                    Log.e("ChatActivity", s);
                    JSONObject jsonObject = new JSONObject(s);
                    JSONObject object = jsonObject.getJSONObject("result");
                    SPUtils.put(ChatActivity.this,"chat","false");
                    bean.setText(object.getString("text"));
                    bean.setMytext(etChat.getText().toString());
                    list.add(bean);
                    adapter.notifyDataSetChanged();
                    Log.e("ChatActivity", "list.size()222222222   :" + list.size());
                    //list.clear();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {

            }
        });
    }
}
